package cn.boge.hrm.service.impl;

import cn.boge.hrm.client.ESCourseClient;
import cn.boge.hrm.constants.CourseConstants;
import cn.boge.hrm.doc.CourseDoc;
import cn.boge.hrm.domain.Course;
import cn.boge.hrm.domain.CourseDetail;
import cn.boge.hrm.domain.CourseMarket;
import cn.boge.hrm.domain.LoginUser;
import cn.boge.hrm.dto.CourseSaveDto;
import cn.boge.hrm.exception.Assert;
import cn.boge.hrm.mapper.CourseDetailMapper;
import cn.boge.hrm.mapper.CourseMapper;
import cn.boge.hrm.mapper.CourseMarketMapper;
import cn.boge.hrm.mapper.CourseTypeMapper;
import cn.boge.hrm.service.ICourseService;
import cn.boge.hrm.util.AjaxResult;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.boge.hrm.context.LoginContext;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author boge
 * @since 2022-04-10
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {


    @Autowired
    private CourseDetailMapper courseDetailMapper;
    @Autowired
    private CourseMarketMapper courseMarketMapper;
    @Autowired
    private ESCourseClient esCourseClient;

    @Override
    public void add(CourseSaveDto courseDto) {
        Course course = courseDto.getCourse();
        CourseDetail courseDetail = courseDto.getCourseDetail();
        CourseMarket courseMarket = courseDto.getCourseMarket();
        //1.基本判断
        //* 2.条件判断 ：比如：课程名不能重复
        String name = course.getName();
        //条件对象
        Wrapper<Course> wrapper = new EntityWrapper();
        wrapper.eq("name",name);
        Course courseFromDB = selectOne(wrapper);
        Assert.assertNull(courseFromDB,"课程名重复");

        //* 3.保存课程，拿到ID
        course.setStatus(CourseConstants.KEY_COURSE_STATUS_OFFLINE);
        LoginUser login = LoginContext.getLogin();

        course.setTenantId(login.getTenantId());
        course.setTenantName(login.getTenantName());
        course.setUserId(login.getId());
        course.setUserName(login.getUsername());
        //TODO
        insert(course);

        //* 4.保存课程营销
        //* 把课程ID设置到营销表
        courseMarket.setId(course.getId());
        courseMarketMapper.insert(courseMarket);

        //* 5.保存课程详情
        //* 把课程ID设置到详情表
        courseDetail.setId(course.getId());
        courseDetailMapper.insert(courseDetail);
    }

    @Override
    @Transactional
    public void onLineCourse(Long id) {
        //1.根据ID查询Mysql中的课程
        Course course = baseMapper.selectById(id);
        //2.判断课程状态为下线才能上线
        if(course == null || course.getStatus() != Course.STATUS_OFFLINE){
            //log.error("课程无效，或状态异常");
            throw new RuntimeException("课程无效，或状态异常");
        }
        //3.修改课程的状态为上线
        course.setStatus(Course.STATUS_ONLINE);
        //4.保存修改的课程到Mysql
        baseMapper.updateById(course);
        //5.保存课程到ES
        //课程营销
        CourseMarket courseMarket = courseMarketMapper.selectById(id);
        //课程详情
        CourseDetail courseDetail = courseDetailMapper.selectById(id);
        //复制属性
        CourseDoc  courseDoc = new CourseDoc();
        BeanUtils.copyProperties(course,courseDoc);
        BeanUtils.copyProperties(courseMarket,courseDoc);
        BeanUtils.copyProperties(courseDetail,courseDoc);
        //关键字搜索字段
        courseDoc.setName(course.getName()+" "+courseDetail.getDescription());
        //其他字段
        courseDoc.setOnlineTime(new Date());
        AjaxResult ajaxResult = esCourseClient.save(courseDoc);
        if(!ajaxResult.isSuccess()){
            //log.error("课程上线失败");
            throw new RuntimeException("课程上线失败");
        }
        //需要用到CourseDoc
    }
}
